addpath(genpath('.'));
addpath(genpath('../liblinear-1.92'));

load_settings;
%source_name = [data_root 'clean_data/collapsed_v2_whiten.mat'];
source_name = 'SANDBOX_RUN1';
data = getData(source_name);
%for ix = 1:size(data)
%
baseline_mat_location = [trained_root 'sr1_model.mat'];


    % holdout cameras....
    % dataset       index       cam id
    % sr1           4           8307
    % sr2           6           1411
    % live          6           1411
    %             
    % iter          5           1411
    
    %num_cams = size(data,1);
    %cams = setdiff(1:num_cams, ix);
[XTR,YTR,XTE,YTE,XTV,YTV,ANN_TR,ANN_TE,ANN_TV] = grabSourceData(source_name, [] );


% settings - these are in load_settings
% settings = {};
% settings.background = 0;
% settings.rks = 0;
% settings.ftimes = 0;
% settings.pca = 0;
% settings.u = [];
% settings.m = [];


% % create model
% disp('Learning baseline model...');
% baseline = learn_baseline(XTR,YTR,XTE,YTE,settings);

XTR = [XTR; XTE];
YTR = [YTR; YTE];
ANN_TR = [ANN_TR, ANN_TE];


% Recommended by libsvm
Cs = [2^-5, 2^-3, 2^-1, 2^1, 2^3, 2^5, 2^7, 2^9, 2^11, 2^13, 2^15];
C = length(Cs);

results = {};
results.TR.acc = zeros(C,1);
results.TR.p    = cell(C,1);
results.TR.dv   = cell(C,1);
results.V.acc  = zeros(C,1);
results.V.p     = cell(C,1);
results.V.dv    = cell(C,1);
results.TE.acc = zeros(C,1);
results.TE.p    = cell(C,1);
results.TE.dv   = cell(C,1);
results.models  = cell(C,1);
results.libsvmmodel = cell(C,1);
	
% class weights
pos  = sum(YTR == 1);
neg = sum(YTR == -1);
	
% [data.XTR, data.YTR] = rebalance(data.XTR, data.YTR);
% [data.XTV, data.YTV] = rebalance(data.XTV, data.YTV);
% [data.XTE, data.YTE] = rebalance(data.XTE, data.YTE);

for c = 1:C % try as a parfor
	%['-c ' num2str(Cs(c)) ' -B 1 -q']);%
	model = train(YTR, sparse(XTR), ['-c ' num2str(Cs(c)) ' -B 1 -w1 ' num2str(neg/(neg+pos)) ' -w-1 ' num2str(pos/(neg+pos)) ' -q']);
	[ptr, ~, dtr] = predict(YTR, sparse(XTR), model); 
	[ptv, ~, dtv] = predict(YTV, sparse(XTV), model); 

	results.TR.acc(c) = sum(ptr == YTR)/length(YTR);
	results.TR.p{c}   = ptr;
	results.TR.dv{c}  = dtr;
	results.V.acc(c)  = sum(ptv == YTV)/length(YTV);
	results.V.p{c}    = ptv;
	results.V.dv{c}   = dtv;
	results.models{c} = model.w';
	results.libsvmmodel{c} = model;
		
end
	
[M,CIX]  = max(results.V.acc);
	
% best = {};
% best.TR.acc = results.TR.acc(CIX);
% best.TR.p   = results.TR.p{CIX};
% best.TR.dv  = results.TR.dv{CIX};
% best.V.acc  = results.V.acc(CIX);
% best.V.p    = results.V.p{CIX};
% best.V.dv   = results.V.dv{CIX};
% best.TE.acc = results.TE.acc(CIX);
% best.TE.p   = results.TE.p{CIX};
% best.TE.dv  = results.TE.dv{CIX};
% best.model  = results.models{CIX};
% best.libsvmmodel = results.libsvmmodel{CIX};
	
% % train on training and validation
%['-c ' num2str(Cs(CIX)) ' -B 1 -q']);
model = train([YTR; YTV], sparse([XTR; XTV]), ['-c ' num2str(Cs(CIX)) ' -B 1 -w1 ' num2str(neg/(neg+pos)) ' -w-1 ' num2str(pos/(neg+pos)) ' -q']);
%['-c 1 -B 1 -w1 ' num2str(neg/(neg+pos)) ' -w-1 ' num2str(pos/(neg+pos)) ' -q']);
% [ptr, ~, dtr] = predict(YTR, sparse(XTR), model); 
% [ptv, ~, dtv] = predict(YTV, sparse(XTV), model); 
	
best = {};
% best.TR.acc = sum(ptr == YTR)/length(YTR);
% best.TR.p   = ptr;
% best.TR.dv  = dtr;
% best.V.acc  = sum(ptv == YTV)/length(YTV);
% best.V.p    = ptv;
% best.V.dv   = dtv;
best.model  = model.w';
best.libsvmmodel = model;
best.C      = Cs(CIX);

save(baseline_mat_location, 'model');

%end
